/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUIv2;

import general.GlobalProperties;

import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JOptionPane;

import serverHandling.HttpRequestHandlers;
import serverHandling.SessionObjects;

import jsonObjects.Files;
import jsonObjects.Profile;

@SuppressWarnings("serial")
public class FileAttributeProfileFileListGUI extends javax.swing.JPanel {

    public FileAttributeProfileFileListGUI(Profile profile) {
        this.profile = profile;

        initComponents();
    }

    private void initComponents() {
        File dir = new File(GlobalProperties.getFolderMonit() + profile.getName());
        File[] activProfTab = dir.listFiles();
        Object[][] futureMenuItems;
        if (activProfTab != null) {
            futureMenuItems = new Object[activProfTab.length][activProfTab.length];
            for (int i = 0; i < activProfTab.length; i++) {
        	if(!activProfTab[i].getName().contains(".properties"))
        	    futureMenuItems[i][0] = activProfTab[i].getName();
            }
        } else
            futureMenuItems = new Object[0][0];
        
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        cancelButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                futureMenuItems,
                new String[]{
                        "Name"
                }
        ) {
            boolean[] canEdit = new boolean[]{
                    false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        cancelButton.setText("Return");
        cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        nextButton.setText("Files list");
        nextButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(0, 5, Short.MAX_VALUE))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addGap(0, 0, Short.MAX_VALUE)
                                                .addComponent(cancelButton)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(nextButton)))
                                .addContainerGap())
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 11, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(nextButton)
                                        .addComponent(cancelButton))
                                .addContainerGap())
        );
    }

    @SuppressWarnings("unused")
    private void jButton1ActionPerformed(java.awt.event.MouseEvent evt) {
        FileAttributesEditGUI replacePanel = null;

        if (jTable1.getSelectedRowCount() == 1) {

            int row = jTable1.getSelectedRow();
            int column = jTable1.getSelectedColumn();
            String cellValue = (String) jTable1.getValueAt(row, column);

            String folderPath = GlobalProperties.getFolderMonit()
                    + profile.getName() + "\\";
            final File chosenFile = new File(folderPath + cellValue);

            if (chosenFile == null)
                JOptionPane
                        .showMessageDialog(null, "Error while loading files");

            else {
                replacePanel = new FileAttributesEditGUI(chosenFile, profile);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Choose file");
        }


        javax.swing.JPanel parenPanel = (javax.swing.JPanel) jScrollPane1.getParent();

        parenPanel.removeAll();
        javax.swing.GroupLayout parenPanelLayot = new javax.swing.GroupLayout(parenPanel);
        parenPanel.setLayout(parenPanelLayot);
        parenPanelLayot.setHorizontalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        parenPanelLayot.setVerticalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
        );
        parenPanel.add(replacePanel);
        parenPanel.revalidate();
        parenPanel.repaint();


    }

    private void jButton2ActionPerformed(MouseEvent evt) {
        FileAttributeReplacePanel replacePanel = new FileAttributeReplacePanel();

        javax.swing.JPanel parenPanel = (javax.swing.JPanel) jScrollPane1.getParent();
        parenPanel.removeAll();

        javax.swing.GroupLayout parenPanelLayot = new javax.swing.GroupLayout(parenPanel);
        parenPanel.setLayout(parenPanelLayot);
        parenPanelLayot.setHorizontalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 420, Short.MAX_VALUE)
        );
        parenPanelLayot.setVerticalGroup(
                parenPanelLayot.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(replacePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
        );
        parenPanel.add(replacePanel);
        parenPanel.revalidate();
        parenPanel.repaint();
    }

    javax.swing.JButton cancelButton;
    javax.swing.JScrollPane jScrollPane1;
    javax.swing.JTable jTable1;
    javax.swing.JButton nextButton;
    Profile profile;
}
